package com.lauor.smpdb.spring.boot.autoconfigure;

import com.lauor.smpdb.SmpDataSource;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 *连接池自动配置
 * @author tfq qq:1394965066
 */
@Configuration
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnClass(SmpDataSource.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)//指定顺序
@EnableConfigurationProperties(DataSourceProperties.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.lauor.smpdb.SmpDataSource",
        matchIfMissing = false)
public class SmpdbAutoConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.smpdb")
    public DataSource dataSource(DataSourceProperties properties){
        LoggerFactory.getLogger(SmpdbAutoConfiguration.class).debug("Init SmpDataSource");

        return properties.initializeDataSourceBuilder().type( SmpDataSourceWrapper.class ).build();
    }
}
